
$(function() {
	Tip.emp('报销人');
	
	$('#emp').blur(function() {
		var a = $(this).val();
		Pay.emp(a);
	});
	
	Pay.emp($('#emp').val());
	
	$('#date').datepicker(_date1_option);
		
	_currency($('#currency').val());
	
	$('#currency').change(function() {
		var s = $(this).val();
		_currency(s);
	});
	
	$('#payment').change(function() {
		var a = $(this).val();
		Pay.payment(a);
	});
	
	Pay.payment($('#payment').val());
	
	$("#label_payment_bank").hide();
	
	$('textarea').css("width", 400);
	
	var grid;
	var columns = [{
	    id: "#",
	    name: "",
	    width: 40,
	    behavior: "selectAndMove",
	    unselectable: true,
	    resizable: false,
	    cssClass: "cell-reorder"
	}, {
	    id: "date",
	    name: "日期",
	    field: "date",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Date
	}, {
	    id: "type",
	    name: "分类",
	    field: "type",
	    width: 80,
	    cssClass: "cell-title",
	    editor: CategorySelectCellEditor
	}, {
	    id: "name",
	    name: "事由",
	    field: "name",
	    width: 260,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "amount",
	    name: "金额",
	    field: "amount",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "ok",
	    name: "",
	    field: "ok",
	    width: 80,
	    cssClass: "cell-effort-driven",	    
	    cannotTriggerInsert: true,
	    formatter: Slick.Formatters.YesNo,
	    editor: Slick.Editors.Checkbox
	}];

	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30    
		};	
	
	var grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
		var item = {date:"", type:"9:其它", name:"", amount:"0.00", ok: true};
		item.date = current_date();
		
	    $.extend(item, args.item);
	    data.push(item);
	    grid.invalidateRows([data.length - 1]);
	    grid.updateRowCount();
	    grid.render();
	});
	
	grid.onCellChange.subscribe(function(e, args) {
		
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
		// The data has changed - recalculate the totals.
	    // dataProvider.updateTotals();

	    // Rerender the totals row (last row).
	    // grid.invalidateRow(dataProvider.getLength() - 1);
	    grid.render();
	    total();
	});
	
	/**
	grid.onAddNewRow = function addItem(newItem, columnDef) {
        var item = {date:"", type:"9:其它", name:"", amount:"0.00", ok: true};
        item.date = current_date();
        
        $.extend(item,newItem);
        data.push(item);
        grid.removeRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
        
        total();
    };
    
    grid.onCellChange = function(currentRow, currentCell, item) {
    	total();
    }; */
});

function _currency(s) {
	if (s == null || s == '-')　{		
		return false;
	}
	
	var body = { };
	body.currency = s;
	$.post('finance.php?do=Finance.ajax_exchange', body, function(s) {
		exchange = s;
		$('#rate').val(s);
		
		_update();
	});
}

function _update() {
	var a = $("#total").val();
	var b = $("#currency").find("option:selected").text();

	Currency.upper(a, b, "#label_total");
		
	_rmb();
}

function _rmb() {
	var a = $("#total").val();
	var b = $('#rate').val();
	var rmb = fxmul(a, b);
	
	$('#rmb').val(rmb);	
}

function total() {
	var total = 0;
	$.each(data, function(k, v) {
		if (v.ok) {
			total += parseFloat(v.amount);
		}		
	});
		
	$("#total_t").html(total);
	$("#total").val(total);
	
	var b = $("#currency").find("option:selected").text();
	Currency.upper(total, b, "#label_total");
}

function CategorySelectCellEditor(args) {
	var $select;
    var defaultValue;
    var scope = this;

    this.init = function() {
        $select = $("<SELECT tabIndex='0' id='type_'></SELECT>");
        
        $select.empty();
        // alert(type_json);
        $.each(type_json, function(k, v) {
        	// alert(k + " = " + v);
        	var item = new Option(v, k);
        	$select[0].options.add(item);
        });
        
        $select.appendTo(args.container);        
        $select.focus();
    };

    this.destroy = function() {
        $select.remove();
    };

    this.focus = function() {
        $select.focus();
    };

    this.loadValue = function(item) {
    	var k = args.column.field;
    	$select.val(item[k]);
    	$select.select();
    };

    this.serializeValue = function() {
        return $select.val();
    };

    this.applyValue = function(item,state) { 
    	var k = args.column.field;
        // item[k] = state;
    	var s = $select.find("option:selected").text();
    	item[k] = state + ":" + s;
    };
   
    this.isValueChanged = function() {
        return ($select.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}
